package com.sbs.gaoxinqutoilet.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Shader;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.util.TypedValue;

/**
 * Created by 聪明一只哈 on 2019/2/25.
 * Time: 2019/2/25  11:40
 */
public class ImageUtils {

    /**
     * 设置水印图片在左上角
     */
    public static Bitmap createWaterMaskLeftTop(Context context, Bitmap src, Bitmap watermark,
                                                int paddingLeft, int paddingTop) {
        return createWaterMaskBitmap(src, watermark, dp_to_px(context, paddingLeft),
                dp_to_px(context, paddingTop));
    }

    /**
     * 设置水印图片到右上角
     */
    public static Bitmap createWaterMaskRightTop(
            Context context, Bitmap src, Bitmap watermark,
            int paddingRight, int paddingTop) {
        return createWaterMaskBitmap(src, watermark,
                src.getWidth() - watermark.getWidth() - dp_to_px(context, paddingRight),
                dp_to_px(context, paddingTop));
    }

    /**
     * 设置水印图片到中间
     */
    public static Bitmap createWaterMaskCenter(Bitmap src, Bitmap watermark) {
        return createWaterMaskBitmap(src, watermark,
                (src.getWidth() - watermark.getWidth()) / 2,
                (src.getHeight() - watermark.getHeight()) / 2);
    }

    /**
     * 设置水印图片到左下角
     */
    public static Bitmap createWaterMaskLeftBottom(
            Context context, Bitmap src, Bitmap watermark,
            int paddingLeft, int paddingBottom) {
        return createWaterMaskBitmap(src, watermark, dp_to_px(context, paddingLeft),
                src.getHeight() - watermark.getHeight() - dp_to_px(context, paddingBottom));
    }

    /**
     * 设置水印图片在右下角
     */
    public static Bitmap createWaterMaskRightBottom(
            Context context, Bitmap src, Bitmap watermark,
            int paddingRight, int paddingBottom) {
        return createWaterMaskBitmap(src, watermark,
                src.getWidth() - watermark.getWidth() - dp_to_px(context, paddingRight),
                src.getHeight() - watermark.getHeight() - dp_to_px(context, paddingBottom));
    }

    /**
     * 添加图片水印
     *
     * @param bitmap
     * @param watermark
     * @param paddingLeft
     * @param paddingTop
     * @return
     */
    private static Bitmap createWaterMaskBitmap(Bitmap bitmap, Bitmap watermark, int paddingLeft, int paddingTop) {
        if (bitmap == null) {
            return null;
        }
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        //创建一个bitmap
        Bitmap newb = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);// 创建一个新的和SRC长度宽度一样的位图
        //将该图片作为画布
        Canvas canvas = new Canvas(newb);
        //在画布 0，0坐标上开始绘制原始图片
        canvas.drawBitmap(bitmap, 0, 0, null);
        //在画布上绘制水印图片
        canvas.drawBitmap(watermark, paddingLeft, paddingTop, null);
        // 保存
        //canvas.save(Canvas.ALL_SAVE_FLAG);
        canvas.save();
        // 存储
        canvas.restore();
        return newb;
    }

    /**
     * 给图片添加文字到左上角
     */
    public static Bitmap drawTextToLeftTop(Context context, Bitmap bitmap, String text,
                                           int size, int color, int paddingLeft, int paddingTop) {
        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setColor(color);
        paint.setTextSize(dp_to_px(context, size));
        Rect bounds = new Rect();
        paint.getTextBounds(text, 0, text.length(), bounds);
        return drawTextToBitmap(context, bitmap, text, paint, bounds,
                dp_to_px(context, paddingLeft),
                dp_to_px(context, paddingTop) + bounds.height());
    }

    /**
     * 给图片添加文字到右上角
     */
    public static Bitmap drawTextToRightTop(Context context, Bitmap bitmap, String text,
                                            int size, int color, int paddingRight, int paddingTop) {
        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setColor(color);
        paint.setTextSize(dp_to_px(context, size));
        Rect bounds = new Rect();
        paint.getTextBounds(text, 0, text.length(), bounds);
        return drawTextToBitmap(context, bitmap, text, paint, bounds,
                bitmap.getWidth() - bounds.width() - dp_to_px(context, paddingRight),
                dp_to_px(context, paddingTop) + bounds.height());
    }

    /**
     * 给图片添加文字到中间
     */
    public static Bitmap drawTextToCenter(Context context, Bitmap bitmap, String text,
                                          int size, int color) {
        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setColor(color);
        paint.setTextSize(dp_to_px(context, size));
        Rect bounds = new Rect();
        paint.getTextBounds(text, 0, text.length(), bounds);
        return drawTextToBitmap(context, bitmap, text, paint, bounds,
                (bitmap.getWidth() - bounds.width()) / 2,
                (bitmap.getHeight() + bounds.height()) / 2);
    }

    /**
     * 给图片添加文字到左下角
     */
    public static Bitmap drawTextToLeftBottom(Context context, Bitmap bitmap, String text,
                                              int size, int color, int paddingLeft, int paddingBottom) {
        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setColor(color);
        paint.setTextSize(dp_to_px(context, size));
        Rect bounds = new Rect();
        paint.getTextBounds(text, 0, text.length(), bounds);
        return drawTextToBitmap(context, bitmap, text, paint, bounds,
                dp_to_px(context, paddingLeft),
                bitmap.getHeight() - dp_to_px(context, paddingBottom));
    }

    /**
     * 给图片添加文字到右下角
     */
    public static Bitmap drawTextToRightBottom(Context context, Bitmap bitmap, String text,
                                               int size, int color, int paddingRight, int paddingBottom) {
        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setColor(color);
        //paint.setColor(ContextCompat.getColor(context,R.color.show));
       paint.setTextSize(dp_to_px(context, size));
        Rect bounds = new Rect();
        paint.getTextBounds(text, 0, text.length(), bounds);

        return drawTextToBitmap(context, bitmap, text, paint, bounds,
                bitmap.getWidth() - bounds.width() - dp_to_px(context, paddingRight),
                bitmap.getHeight() - dp_to_px(context, paddingBottom));
    }


    /**
     * @param context
     * @param bitmap
     * @param text
     * @param paint
     * @param bounds
     * @param paddingLeft
     * @param paddingTop
     * @return
     */
    private static Bitmap drawTextToBitmap(Context context, Bitmap bitmap, String text,
                                           Paint paint, Rect bounds, int paddingLeft, int paddingTop) {
        Bitmap.Config bitmapConfig = bitmap.getConfig();

        paint.setDither(true); // 获取跟清晰的图像采样
        paint.setFilterBitmap(true);// 过滤一些
        if (bitmapConfig == null) {
            bitmapConfig = Bitmap.Config.ARGB_8888;
        }
        bitmap = bitmap.copy(bitmapConfig, true);
        Canvas canvas = new Canvas(bitmap);

        if(bounds.width()>bitmap.getWidth()){

            String[] split = text.split(";");
            //drawTextToBitmap: 2019-08-08~~~北京市北京市房山区大件路甲8号靠近北京金融安全产业园~~~经度:116.04300369597013纬度:39.71518469360647
            //drawTextToBitmap: 2019-08-08~~~~~~经度:116.04300369597013纬度:39.71518469360647
            if(split.length>2){
                String content1=split[0];
                String content2=split[1];
                String content3=split[2];
//            String content1=text.substring(0,text.length()/2);
//            String content2=text.substring(text.length()/2,text.length());
//            String content3=text.substring(text.length()/2,text.length());
                Log.i("标记内容", "drawTextToBitmap: "+content1+"~~~"+content2+"~~~"+content3);

//            Rect bound1 = new Rect();
//            Paint paint1 = new Paint();
//            paint1.getTextBounds(content1, 0, content1.length(), bound1);
//
//            canvas.drawText(content1, bitmap.getWidth()/2- bound1.width()/2, paddingTop-(bitmap.getHeight()-paddingTop)*2-20, paint);
                if(content2.length()<20){
                    canvas.drawText(content1, 20, paddingTop-(bitmap.getHeight()-paddingTop)*3-60, paint);
                    canvas.drawText(content2, 20, paddingTop-(bitmap.getHeight()-paddingTop)*2-20, paint);
                }else{


                    String contentTwo1=content2.substring(0,content2.length()/2);
                    String contentTwo2=content2.substring(content2.length()/2,content2.length());

                    Log.i("标记内容", "drawTextToBitmap: "+content1+"~~~"+contentTwo1+"~~~"+contentTwo2);

                    canvas.drawText(content1, 20, paddingTop-(bitmap.getHeight()-paddingTop)*4-100, paint);
                    canvas.drawText(contentTwo1, 20, paddingTop-(bitmap.getHeight()-paddingTop)*3-60, paint);
                    canvas.drawText(contentTwo2, 20, paddingTop-(bitmap.getHeight()-paddingTop)*2-10, paint);
                }
//            Rect bound2 = new Rect();
//            Paint paint2 = new Paint();
//            paint2.getTextBounds(content2, 0, content2.length(), bound2);
//            canvas.drawText(content2, bitmap.getWidth()/2- bound2.width()/2, paddingTop, paint);
                canvas.drawText(content3, 20, paddingTop, paint);
            }else{
                String content1=split[0];
                String content2=split[1];
//            String content1=text.substring(0,text.length()/2);
//            String content2=text.substring(text.length()/2,text.length());
//            String content3=text.substring(text.length()/2,text.length());
                Log.i("标记内容", "drawTextToBitmap: "+content1+"~~~"+content2+"~~~"+"缺少内容");

//            Rect bound1 = new Rect();
//            Paint paint1 = new Paint();
//            paint1.getTextBounds(content1, 0, content1.length(), bound1);
//
//            canvas.drawText(content1, bitmap.getWidth()/2- bound1.width()/2, paddingTop-(bitmap.getHeight()-paddingTop)*2-20, paint);
                if(content2.length()<20){
                    canvas.drawText(content1, 20, paddingTop-(bitmap.getHeight()-paddingTop)*3-60, paint);
                    canvas.drawText(content2, 20, paddingTop-(bitmap.getHeight()-paddingTop)*2-20, paint);
                }else{


                    String contentTwo1=content2.substring(0,content2.length()/2);
                    String contentTwo2=content2.substring(content2.length()/2,content2.length());

                    Log.i("标记内容", "drawTextToBitmap: "+content1+"~~~"+contentTwo1+"~~~"+contentTwo2);

                    canvas.drawText(content1, 20, paddingTop-(bitmap.getHeight()-paddingTop)*4-100, paint);
                    canvas.drawText(contentTwo1, 20, paddingTop-(bitmap.getHeight()-paddingTop)*3-60, paint);
                    canvas.drawText(contentTwo2, 20, paddingTop-(bitmap.getHeight()-paddingTop)*2-10, paint);
                }
            }

        }else{
            canvas.drawText(text, paddingLeft, paddingTop, paint);
        }

//        String content1=text.substring(0,text.length()/2);
////        String content2=text.substring(text.length()/2,text.length());
//        canvas.drawText(content1, bitmap.getWidth()/2, paddingTop-(bitmap.getHeight()-paddingTop)*2-20, paint);
//
//        canvas.drawText(text, paddingLeft, paddingTop, paint);






        return bitmap;
    }


    /**
     * dip转pix
     *
     * @param context
     * @param dp
     * @return
     */
    public static int dp_to_px(Context context, float dp) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dp * scale + 0.5f);
    }


    /**
     * 获得倾斜的浮水印
     * @param gContext
     * @param gText
     * @param width
     * @param height
     * @param is4Showing
     * @return
     */
    public static Bitmap getMarkTextBitmap(Context gContext, String gText, int width, int height, boolean is4Showing) {
//  Bitmap bitmap = ACache.get(gContext).getAsBitmap(gText);
//  if (is4Showing && bitmap != null){
//   return bitmap;
//  }

        float textSize;
        float inter;
        if (is4Showing) {
            textSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 18, gContext.getResources().getDisplayMetrics());
            inter = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 25, gContext.getResources().getDisplayMetrics());
        } else {
            textSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 54, gContext.getResources().getDisplayMetrics());
            inter = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 75, gContext.getResources().getDisplayMetrics());
        }

        int sideLength;
        if (width > height) {
            sideLength = (int) Math.sqrt(2 * (width * width));
        } else {
            sideLength = (int) Math.sqrt(2 * (height * height));
        }


        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        Rect rect = new Rect();
        paint.setTextSize(textSize);
        //获取文字长度和宽度
        paint.getTextBounds(gText, 0, gText.length(), rect);

        int strwid = rect.width();
        int strhei = rect.height();

        Bitmap markBitmap = null;
        try {
            markBitmap = Bitmap.createBitmap(sideLength, sideLength, Bitmap.Config.ARGB_4444);
            Canvas canvas = new Canvas(markBitmap);
            //创建透明画布
            canvas.drawColor(Color.TRANSPARENT);

            paint.setColor(Color.BLACK);
            paint.setAlpha((int) (0.1 * 255f));
            // 获取跟清晰的图像采样
            paint.setDither(true);
            paint.setFilterBitmap(true);

            //先平移，再旋转才不会有空白，使整个图片充满
            if (width > height) {
                canvas.translate(width - sideLength - inter, sideLength - width + inter);
            } else {
                canvas.translate(height - sideLength - inter, sideLength - height + inter);
            }

            //将该文字图片逆时针方向倾斜45度
            canvas.rotate(-45);

            for (int i = 0; i <= sideLength; ) {
                int count = 0;
                for (int j = 0; j <= sideLength; count++) {
                    if (count % 2 == 0) {
                        canvas.drawText(gText, i, j, paint);
                    } else {
                        //偶数行进行错开
                        canvas.drawText(gText, i + strwid / 2, j, paint);
                    }
                    j = (int) (j + inter + strhei);
                }
                i = (int) (i + strwid + inter);
            }
            //canvas.save(Canvas.ALL_SAVE_FLAG);
            canvas.save();
//  ACache.get(gContext).put(gText, markBitmap);
        } catch (OutOfMemoryError e) {

            if (markBitmap != null && !markBitmap.isRecycled()) {
                markBitmap.recycle();
                markBitmap = null;
            }
        }

        return markBitmap;
    }

    /**
     * 获得文字水印的图片
     * @param width
     * @param height
     * @return
     */
    public static Drawable getMarkTextBitmapDrawable(Context gContext, String gText, int width, int height, boolean is4Showing){
        Bitmap bitmap = getMarkTextBitmap(gContext, gText, width, height, is4Showing);
        if (bitmap != null){
            BitmapDrawable drawable = new BitmapDrawable(gContext.getResources(), bitmap);
            drawable.setTileModeXY(Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
            drawable.setDither(true);
//   Drawable drawableFinal = drawable.getConstantState().newDrawable();
//   if(!bitmap.isRecycled()){
//    bitmap.recycle();
//    bitmap = null;
//   }
//
            return drawable;
        }
        return null;
    }
}
